<template>
  <!--
    秒杀成功订单 功能列表

    @author wangyongkang ${authorEmail}
    @date 2022-03-17 23:48:40
  -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="秒杀结果" prop="status">
        <el-select v-model="queryParams.status" placeholder="全部" clearable>
          <el-option
            v-for="item in StatusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ItemKillSuccessList" @selection-change="handleSelectionChange" border height="800">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="code" sortable/>
      <el-table-column label="商品名称" align="center" prop="itemId" :formatter="formatterClassify"/>
<!--      <el-table-column label="秒杀ID" align="left" prop="killId" />-->
      <el-table-column label="用户ID" align="center" prop="userId" sortable/>
      <el-table-column label="秒杀结果" align="center" prop="status" >
        <template slot-scope="scope">
          <div v-if="scope.row.status === -1" style="color:#ef084e">无效</div>
          <div v-if="scope.row.status === 0" style="color:green"> 成功(未付款)</div>
          <div v-if="scope.row.status === 1" style="color:#1180e2">已付款</div>
          <div v-if="scope.row.status === 2" style="color:rgba(250,122,8,0.78)">已取消</div>
        </template>

      </el-table-column>

      <el-table-column label="下单时间" align="center" prop="addTime" sortable>
        <template slot-scope="scope">
          <span>{{ parseDatetime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改秒杀成功订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="秒杀成功生成的订单编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入秒杀成功生成的订单编号" />
        </el-form-item>
        <el-form-item label="商品id" prop="itemId">
          <el-input v-model="form.itemId" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="秒杀id" prop="killId">
          <el-input v-model="form.killId" placeholder="请输入秒杀id" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="秒杀结果: -1无效  0成功(未付款)  1已付款  2已取消" prop="status">
          <el-input v-model="form.status" placeholder="请输入秒杀结果: -1无效  0成功(未付款)  1已付款  2已取消" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listItemKillSuccess,
  getItemKillSuccess,
  delItemKillSuccess,
  addItemKillSuccess,
  changeItemKillSuccessField,
  changeStatusItemKillSuccess,
  updateItemKillSuccess,
  exportItemKillSuccess } from "@/api/shop-manage/ItemKillSuccess";
import  Condition  from "alinesno-ui/packages/Search/Condition"
import {ItemList} from "@/api/shop-manage/Item";

export default {
  name: "ItemKillSuccess",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 状态
      statusOptions:[] ,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 秒杀成功订单表格数据
      ItemKillSuccessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        itemId: null,
        killId: null,
        userId: null,
        status: null,
      },
      // 查询参数配置对象
      queryParamsConfig: {
        code: Condition.like(),
        itemId: Condition.like(),
        killId: Condition.like(),
        userId: Condition.like(),
        status: Condition.like(),
      },

      //状态
      StatusOptions: [{
        value: -1,
        label: '无效'
      }, {
        value: 0,
        label: '成功(未付款)'
      },
        {
          value: 1,
          label: '已付款'
        },
        {
          value: 2,
          label: '已取消'
        }],
      itemList:null,
      itemTotal:0,
      // 搜索参数
      searchParams:[] ,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "秒杀成功生成的订单编号不能为空", trigger: "blur" }
        ],
        itemId: [
          { required: true, message: "商品id不能为空", trigger: "blur" }
        ],
        killId: [
          { required: true, message: "秒杀id不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "秒杀结果: -1无效  0成功(未付款)  1已付款  2已取消不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getItemList();
    // 查询公共状态
    this.getDicts("has_status").then(response => {
      this.statusOptions = response.data;
    });

  },
  methods: {
    /** 查询秒杀成功订单列表 */
    getList() {
        this.searchParams = this.searchParam(this.queryParamsConfig, this.queryParams);
        this.loading = true;
      listItemKillSuccess(this.searchParams).then(response => {
        this.ItemKillSuccessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取商品列表
    getItemList(){
      ItemList().then((res)=>{
        this.itemList=res.rows;
        this.itemTotal=res.total;
      });
      console.log('this.itemTotal',this.itemTotal);
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        code: null,
        itemId: null,
        killId: null,
        userId: null,
        status: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // 获取参数
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加秒杀成功订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const code = row.id || this.ids
      getItemKillSuccess(code).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改秒杀成功订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItemKillSuccess(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItemKillSuccess(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const codes = row.id || this.ids;
      this.$confirm('是否确认删除秒杀成功订单编号为"' + codes + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delItemKillSuccess(codes);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 状态修改 **/
    handleStatusChange(row) {
      return changeStatusItemKillSuccess(row.id, row.status).then(response=>{
        if(response.code == 200){
          this.msgSuccess("操作成功");
        }
      });
    },
    /** 修改字段状态 **/
    chanageFile(value , filed , id){
      return changeItemKillSuccessField(value , filed , id).then(response =>{
        if(response.code == 200){
          this.msgSuccess("操作成功");
        }
      }) ;
    } ,
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有秒杀成功订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportItemKillSuccess(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    formatterClassify(row) {
      let index = this.itemList.findIndex(item => item.id === row.itemId);
      return index === -1 ? "--" : this.itemList[index].name;
    }
  }
};
</script>
